package br.com.tbp.main;

import javax.swing.*;
import java.awt.*;

public class SplashScreen extends JWindow {

    BorderLayout borderLayout1 = new BorderLayout();
    BorderLayout borderLayout2 = new BorderLayout();
    BorderLayout borderLayout3 = new BorderLayout();
    JLabel imageLabel = new JLabel();
    JPanel southPanel = new JPanel();
    JPanel borderPanel = new JPanel();
    JProgressBar progressBar = new JProgressBar();
    ImageIcon imageIcon;

    public SplashScreen(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // note - this class created with JBuilder
    void jbInit() throws Exception {
        imageLabel.setIcon(imageIcon);
        this.getContentPane().setLayout(borderLayout1);
        southPanel.setLayout(borderLayout2);
        borderPanel.setLayout(borderLayout3);
        southPanel.add(progressBar, "Center");

        borderPanel.add(southPanel, "South");
        borderPanel.add(imageLabel, "Center");

        borderPanel.setBorder(BorderFactory.createLineBorder(new Color(195, 223, 251)));
        //borderPanel.setBorder(BorderFactory.createLineBorder(new Color(32, 88, 54)));
        this.getContentPane().add(borderPanel, "Center");
        this.pack();
    }

    public void setProgressMax(int maxProgress) {
        progressBar.setMaximum(maxProgress);
    }

    public void setProgress(int progress) {
        final int theProgress = progress;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                progressBar.setValue(theProgress);
            }
        });
    }

    public void setProgress(String message, int progress) {
        final int theProgress = progress;
        final String theMessage = message;
        setProgress(progress);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                progressBar.setValue(theProgress);
                setMessage(theMessage + " %");
            }
        });
    }

    public void setScreenVisible(boolean b) {
        final boolean boo = b;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                setVisible(boo);
            }
        });
    }

    private void setMessage(String message) {
        if (message == null) {
            message = "";
            progressBar.setStringPainted(false);
        } else {
            progressBar.setStringPainted(true);
        }
        progressBar.setString(message);
    }
}
